package com.bwie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.ScenMapper;
import com.bwie.mapper.UserMapper;
import com.bwie.pojo.First;
import com.bwie.pojo.Scend;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author DarkClown
 * @date 2024-07-23 09:07
 * @project_name week0723
 * @parem
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    ScenMapper scenMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public R login(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        User user1 = userMapper.selectOne(wrapper.lambda().eq(User::getUsername, user.getUsername()));

        if (!user.getPassword().equals(user1.getPassword())){
            return R.OK("登录失败");
        }


        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456")
                .claim("uid", user1.getUid())
                .compact();

        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();

        opsForValue.set("token"+user1.getUid(),token);

        return R.OK(token);

    }

    @Override
    public R selectList(PageInfoVo pageInfoVo) {
        Page<Scend> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        QueryWrapper<Scend> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(pageInfoVo.getKeyWord())){

            queryWrapper.lambda().like(Scend::getScenName,pageInfoVo.getKeyWord());

        }
        if (!StringUtils.isEmpty(pageInfoVo.getNum())){

            queryWrapper.lambda().eq(Scend::getScenNum,pageInfoVo.getNum());

        }
        queryWrapper.lambda().orderByAsc(Scend::getOrderss);


        Page<Scend> scendPage = scenMapper.selectPage(page,queryWrapper);

        HashMap<String, Object> map = new HashMap<>();

        map.put("total",scendPage.getRecords().size());
        map.put("pageNum",pageInfoVo.getPageNum());
        map.put("pageSize",pageInfoVo.getPageSize());
        map.put("list",scendPage.getRecords());

        return R.OK(map);


    }

    @Override
    public R insertScend(Scend scend) {
        scend.setCreateTime(new Date());
        scend.setUpdateTime(new Date());
        First first = scenMapper.selectFirst(scend.getFirId());

            scend.setFirName(first.getFirName());

        int insert = scenMapper.insert(scend);
        return R.OK();
    }

    @Override
    public R deleteById(Integer id) {
        First first = scenMapper.selectFirst(id);
        if (!BeanUtil.isEmpty(first)){
            return R.OK("当前组织，存在下级组织，请先删除下级组织");
        }
        int i = scenMapper.deleteById(id);

        scenMapper.deleteFormFirst(id);
        return R.OK();
    }

    @Override
    public List<First> selectFirList() {
        return scenMapper.FirList();
    }


}
